package com.joven.core.constant;

import lombok.Data;

import java.io.Serializable;

/**
 * 返回结果类统一封装
 *
 * @author Administrator
 */
@Data
public class ResultInfo<T> implements Serializable {

    // 状态码
    private String code;
    // 消息
    private String message;
    // 数据对象
    private T result;

    public ResultInfo() {
//        super();
    }

    public ResultInfo(Status status) {
//        super();
        this.code = status.code;
        this.message = status.message;
    }

    public ResultInfo result(T result) {
        this.result = result;
        return this;
    }

    public ResultInfo message(String message) {
        this.message = message;
        return this;
    }

    /**
     * 只返回状态，状态码，消息
     */
    public ResultInfo(String code, String message) {
//        super();
        this.code = code;
        this.message = message;
    }

    /**
     * 只返回状态，状态码，数据对象
     */
    public ResultInfo(String code, T result) {
//        super();
        this.code = code;
        this.result = result;
    }

    /**
     * 返回全部信息即状态，状态码，消息，数据对象
     */
    public ResultInfo(String code, String message, T result) {
//        super();
        this.code = code;
        this.message = message;
        this.result = result;
    }


    public static <T> ResultInfo<T> success() {
        return new ResultInfo<>(Status.SUCCESS.getCode(), null, null);
    }

    public static <T> ResultInfo<T> success(T data) {
        return new ResultInfo<>(Status.SUCCESS.getCode(), data);
    }

    public static <T> ResultInfo<T> success(String message, T data) {
        return new ResultInfo<>(Status.SUCCESS.getCode(), message, data);
    }

    /**
     * @param message 错误信息
     * @return ResultInfo
     */
    public static <T> ResultInfo<T> fail(String message) {
        return new ResultInfo<>(Status.FAIL.getCode(), message);
    }

    public static <T> ResultInfo<T> fail(Status status) {
        return new ResultInfo<>(status.getCode(), status.getMessage());
    }

    /**
     * @param message    错误信息
     * @param resultCode 结果码
     * @return ResultInfo
     */
    public static <T> ResultInfo<T> fail(Status resultCode, String message) {
        return new ResultInfo<>(resultCode.getCode(), message);
    }

    public static <T> ResultInfo<T> fail(String errorCode, String message) {
        return new ResultInfo<>(errorCode, message);
    }

    /**
     * @param data       错误信息对象
     * @param resultCode 结果码
     * @return ResultInfo
     */
    public static <T> ResultInfo<T> fail(Status resultCode, T data) {
        return new ResultInfo<>(resultCode.getCode(), data);
    }
}
